package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.CourseConstants;
import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.exception.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "service-common-ordersystemmessage-consumer",
        //Topic
        topic = OrderConstants.TOPIC_ORDER_SYSTEM_MESSAGE_NAME,
         //TAGS
        selectorExpression = OrderConstants.TAGS_ORDER_SYSTEM_MESSAGE_NAME
)
@Slf4j
public class OrderSystemMessageMQConsumer implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt message) {

        try {
            byte[] body = message.getBody();
            String bodyStr = new String(body, Constants.UTF8);
//            List<Long> aLong = JSON.parseArray(bodyStr, Long.class);
            System.out.println(bodyStr);
            //1.保持数据到数据库
            //2.发送邮件
//            System.out.println("站内推送...");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            //通过异常来决定ACK
            throw new GlobalException("站内推送失败");
        }
    }
}